{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-13-c31183523679>, line 20)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-13-c31183523679>\"\u001b[1;36m, line \u001b[1;32m20\u001b[0m\n\u001b[1;33m    new_filename = f\"{year_month}-{branch_file.stem}.csv\"\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "def organize_files(folder_path) :\n",
    "    sales_fo1der = Path(folder_path)\n",
    "    \n",
    "    #遍历月份文件夹\n",
    "    for month_folder in sales_folder.iterdir():\n",
    "        if month_folder.is_dir():\n",
    "             #遍历分店文件\n",
    "            for branch_fi1e in month_folder.iterdir():\n",
    "                if branch_file.is_file():\n",
    "                    #获取文件的最近修改时间\n",
    "                    modified_time = datetime.fromtimestamp(os. path. getmtime(branch_file))\n",
    "                    year_month = modified_time.strftime('%Y年%m月')\n",
    "                    \n",
    "                    #创建新的分店文件夹\n",
    "                    new_branch_folder = sales_folder / branch_file.stem\n",
    "                    new_branch_folder.mkdir(parents=True,exist_ok=True\n",
    "                                            \n",
    "                    #构建新的文件名\n",
    "                    new_filename = f\"{year_month}-{branch_file.stem}.csv\"\n",
    "                                            \n",
    "                    #移动并重命名文件\n",
    "                    new_file_path = new_branch_folder / new_filename\n",
    "                    branch_file.rename (new_file_path)\n",
    "                                            \n",
    "            #删除空的月份文件夹\n",
    "            month_folder. rmdir()\n",
    "#使用示例\n",
    "if __name__ == \"_main_\" :\n",
    "    folder_to_organize = \"C:\\\\Users\\\\Administrator\\\\Desktop\\\\销售表\"\n",
    "    organize_files(folder_to_organize)\n",
    "    print(\"文件已批量整理完成!\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
